	
var isloading = false;
var page = 0;
$(document).ready(function() {
	getUserInfo();
	$('#loading').click();
	//initMyScroll();
});


/**
 * 初始化用户积分信息
 */
function getUserInfo(){
	var url = window.rootPath + '/ui/exchange/getUser';
	jQuery.ajax({
		url : encodeURI(url),
		cache : false,
		type : "get",
		data : "",
		dataType : "json",
		contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
		success : function(data) {
			jQuery('#contentTitle').text(data.contentTitle);
			jQuery('#contentNum').text(data.contentNum);
		}
	})
}

function getGoods(){
	var url = window.rootPath + '/ui/exchange/getGoodsPage/'+page;
	jQuery.ajax({
		url : encodeURI(url),
		cache : false,
		type : "get",
		data : "",
		dataType : "json",
		contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
		success : function(data) {
			var goods = data.goods;
			if(goods.length > 0){
				var listView = $("#add");
	            for (var i = 0; i < goods.length; i++) {
		        		var g = goods[i];
		        		
		        		var purl=g.iconAddress;
		        		var memo1 = "商品名:"+g.goodsName;
		        		var memo2 = g.goodsMemo;
		        		var memo3 = '积分:'+g.goodsPrice;
	//	        		var memo4 = '截止日期:'+moment(g.colsingDate).format('YYYY-MM-DD HH:mm');
		        		listView.setListViewContent(g.id,purl,memo1,memo2,memo3);
	            }
	            page++;
	            isloading = false;
	            $('#loading').toggleClass('loading');
		    		$('.loadingLabel').html('加载更多...');   
			}else{
				isloading = false;
	            $('#loading').toggleClass('loading');
		    		$('.loadingLabel').html('无更多商品加载...');   
			}
		}
	})
}

$('#loading').click(function(){
	if(!isloading){
		isloading = true;
		$('#loading').toggleClass('loading');
		$('.loadingLabel').html('Loading...');    
		getGoods();
	}
})

$("body").on("click",".hrefClick",function() {
	var id = $(this).attr("defineId");
	
	var url = window.rootPath + '/ui/exchange/getGoods/'+id;
	jQuery.ajax({
		url : encodeURI(url),
		cache : false,
		type : "get",
		data : "",
		dataType : "json",
		contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
		success : function(data) {
			var goods = data.goods;
			
			var iconAddress = goods.iconAddress;
			if (iconAddress == undefined || iconAddress == "") {
				iconAddress = "img/nopicture.png";
			}
			iconAddress = window.rootPath + "/" + iconAddress;
			$('#iconAddress').attr('src',iconAddress);
			
			var goodsName = goods.goodsName;
			if(!goodsName) goodsName = "";
			$('#goodsName').html(goodsName);

			var goodsMemo = goods.goodsMemo;
			if(!goodsMemo) goodsMemo = "";
			$('#goodsMemo').html(goodsMemo);

			var goodsPrice = goods.goodsPrice;
			if(!goodsPrice) goodsPrice = "";
			$('#goodsPrice').html(goodsPrice);
			
			var id = goods.id;
			if(!id) id = "";
			$('#id').val(id);
				
		}
	})
	
	$(this).prop("href",  "#goodsPage");
})

$(document).on("click","#exchangeBtn",function() {
	var id = $("#id").val().trim();
	if (confirm("确认兑换 "+ $('#goodsName').html() +" 该商品?")) {
		
		if(_phoneType=="Android"){
			window.jump.proShow(); 
		}else if(_phoneType=="Ios"){
			window.webkit.messageHandlers.submitWaiting.postMessage({body: 'open'});
		}else{
			$("#popupView").popup("open");
		}
		
		$.ajax({
			url : rootPath + "/ui/exchange/exchangeGoods/"+ id,
			dataType : 'json',
			type : 'GET',
			success : function(result) {
				
				if(_phoneType=="Android"){
					window.jump.proDismiss(); 
				}else if(_phoneType=="Ios"){
					window.webkit.messageHandlers.submitWaiting.postMessage({body: 'close'});
				}else{
					$("#popupView").popup("close");
				}
				alert(result.msg);
				getUserInfo();
				if(result.code == 0){
					setTimeout(function(){$('#goodsPageBack').click();},500);
				}
			}
		});
	}
})

$("body").on("click", ".contentClick", function (){
	
	$.ajax({
		url : rootPath + "/ui/exchange/exchangeDetail",
		dataType : 'json',
		type : 'GET',
		success : function(result) {
			var data = result.exchange;
			var content ='';
			for (var i = 0; i < data.length; i++) {
				content += '<tr><td>'+moment(data[i].operationTime).format('YYYY-MM-DD HH:mm')+'</td><td>'+data[i].goods.goodsName+'</td><td>'+data[i].rewardValue+'</td><td>-'+data[i].redeemCode+'</td></tr>';
			}
			jQuery('#rewardTbody').html(content);
		}
	});
	$(this).prop("href", "#exchangeDetail");
});
